package com.cky.forum.controller;

import com.cky.forum.common.AppResult;
import com.cky.forum.common.ResultCode;
import com.cky.forum.config.AppConfig;
import com.cky.forum.model.User;
import com.cky.forum.services.IUserService;
import com.cky.forum.util.MD5Util;
import com.cky.forum.util.StringUtil;
import com.cky.forum.util.UUIDUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
//日志注解
@Slf4j
//这是一个返回数据的controller
@RestController
//路由映射，一级路径
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;
    @RequestMapping("/register")
    public AppResult register(String username,String nickname,
                              String password,String passwordRepeat){
        if(StringUtil.isEmpty(username)||StringUtil.isEmpty(nickname)
        ||StringUtil.isEmpty(password)||StringUtil.isEmpty(passwordRepeat)){
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //校验尼玛是否相等
        if(!password.equals(passwordRepeat)){
            log.warn(ResultCode.FAILED_TWO_PWD_NOT_SAME.toString());
            return AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }
        //准备数据
        User user = new User();
        user.setUsername(username);
        user.setNickname(nickname);
        //生成盐
        String salt = UUIDUtil.UUID_32();
        String encryptPassword = MD5Util.md5Salt(password,salt);
        user.setPassword(encryptPassword);
        user.setSalt(salt);
        //调用service层
        userService.createNormalUser(user);
        return AppResult.success();
    }
    @RequestMapping("/login")
    public AppResult login(HttpServletRequest request,String username, String password){
        //1、调用service中的登陆方法，返回user对象
        User user = userService.login(username,password);
        if(user == null){
            log.warn(ResultCode.FAILED_LOGIN.toString());
            return AppResult.failed(ResultCode.FAILED_LOGIN);
        }
        //2、如果登录成功把user对象设置到session作用域中
        HttpSession session = request.getSession(true);
        session.setAttribute(AppConfig.USER_SESSION,user);
        //3、返回结果
        return AppResult.success();
    }
    @GetMapping("/info")
    public AppResult<User> getUserInfo(HttpServletRequest request,
                                       @RequestParam(value = "id",required = false) Long id){
        User user = null;
        //1、如果id为空，那么从session中获取当前登录用户的信息
        if(id == null){
            HttpSession session = request.getSession(false);
            //判断session是否存在
            if(session == null || session.getAttribute(AppConfig.USER_SESSION) == null){
                return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
            }
            user = (User) session.getAttribute(AppConfig.USER_SESSION);
        }
        else{
            user = userService.selectById(id);
        }
        if(user == null){
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        return AppResult.success(user);
    }
    @RequestMapping("/logout")
    public AppResult logout(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session != null){
            log.info("退出成功");
            //表示用户在登录状态
            session.invalidate();

        }
        return AppResult.success("退出成功");
    }
    @RequestMapping("/modifyInfo")
    public AppResult modifyInfo(HttpServletRequest request
                                ,String username,
                                String nickname,
                                Byte gender,
                                String email,
                                String phoneNum,
                                String remark){
        if(StringUtil.isEmpty(username) && StringUtil.isEmpty(nickname)
        && StringUtil.isEmpty(email) && StringUtil.isEmpty(phoneNum)
        && StringUtil.isEmpty(remark) && gender == null){
            return  AppResult.failed("请输入需要修改的内容");

        }
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        User updateUser = new User();
        updateUser.setId(user.getId());
        updateUser.setUsername(username);
        updateUser.setNickname(nickname);
        updateUser.setEmail(email);
        updateUser.setPhoneNum(phoneNum);
        updateUser.setRemark(remark);
        userService.modifyInfo(updateUser);
        user = userService.selectById(user.getId());
        session.setAttribute(AppConfig.USER_SESSION,user);
        return  AppResult.success(user);
    }
    @RequestMapping("/modifyPwd")
    public  AppResult modifyPassword(HttpServletRequest request,
                                     String oldPassword,
                                     String newPassword,
                                     String passwordRepeat){
        if(!newPassword.equals(passwordRepeat)){
            return AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        userService.modifyPassword(user.getId(),newPassword,oldPassword);
        if (session != null){
            session.invalidate();
        }
        return AppResult.success();
    }
}

